﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Linq;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public class YARNSummary
    {
        public string ARTICLE;
        public string TYPE;
        public string YARN;
        public string YARNDES;
        public string COLOR;
        public string COLORDES;
        public double REQUESTWEI;
        public YARNSummary()
        {
        }
        public YARNSummary(string article, string type, string yarn, string yarndes, string color, string colordes, double requestwei)
        {

            ARTICLE = article;
            TYPE = type;
            YARN = yarn;
            YARNDES = yarndes;
            COLOR = color;
            COLORDES = colordes;
            REQUESTWEI = requestwei;
        }

    }
    public class YARNREQUESTData : BaseFunction
    {
        public bool flagRegenerateLastMonthData;
        ArrayList ListTris = new ArrayList();

        List<YARNSummary> ListYARNSummary;
        string TrisInclude = "";

        public int ThreadCount;
        private string OrderNum;
        DataTable tbYARNSummary;
        public YARNREQUESTData()
            : base()
        {
            BuildTrisList();
            tbYARNSummary = new DataTable("YARNREQSUM");

            tbYARNSummary.Columns.Add("ARTICLE", typeof(string));
            tbYARNSummary.Columns.Add("TYPE", typeof(string));
            tbYARNSummary.Columns.Add("YARN", typeof(string));
            tbYARNSummary.Columns.Add("YARNDES", typeof(string));
            tbYARNSummary.Columns.Add("COLOR", typeof(string));
            tbYARNSummary.Columns.Add("COLORDES", typeof(string));
            tbYARNSummary.Columns.Add("REQUESTWEI", typeof(double));
        }
        public YARNREQUESTData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        private void BuildTrisList()
        {
            ListTris = new ArrayList();
            TrisType tristype = null;
            TrisInclude = "01;10;11;13;32;27;21;";
            tristype = new TrisType("01", "+", "净长"); ListTris.Add(tristype);
            tristype = new TrisType("10", "+", "货物收入"); ListTris.Add(tristype);
            tristype = new TrisType("11", "+", "货物收入"); ListTris.Add(tristype);
            tristype = new TrisType("12", "-", "销售返回"); ListTris.Add(tristype);
            tristype = new TrisType("13", "+", "产品录入"); ListTris.Add(tristype);
            tristype = new TrisType("20", "-", "货物发出"); ListTris.Add(tristype);
            tristype = new TrisType("21", "+", "货物进入"); ListTris.Add(tristype);
            tristype = new TrisType("26", "-", "旧布卷减少"); ListTris.Add(tristype);
            tristype = new TrisType("27", "+", "新布卷增加"); ListTris.Add(tristype);
            tristype = new TrisType("31", "-", "发货"); ListTris.Add(tristype);
            tristype = new TrisType("32", "+", "退货"); ListTris.Add(tristype);
            tristype = new TrisType("36", "-", "生产消耗"); ListTris.Add(tristype);
            tristype = new TrisType("40", "-", "等级修改减少"); ListTris.Add(tristype);
            tristype = new TrisType("41", "-", "等级修改增加"); ListTris.Add(tristype);
            tristype = new TrisType("42", "+", "库存修正减少"); ListTris.Add(tristype);
            tristype = new TrisType("43", "+", "库存修正增加"); ListTris.Add(tristype);
            tristype = new TrisType("48", "-", "生产差异-"); ListTris.Add(tristype);
            tristype = new TrisType("49", "+", "生产差异+"); ListTris.Add(tristype);
            tristype = new TrisType("51", "+", "消耗差异"); ListTris.Add(tristype);
            tristype = new TrisType("61", "-", "留位"); ListTris.Add(tristype);
            tristype = new TrisType("62", "+", "取消留位"); ListTris.Add(tristype);
            tristype = new TrisType("64", "+", "留位剪布"); ListTris.Add(tristype);
            tristype = new TrisType("65", "+", "锁定"); ListTris.Add(tristype);
            tristype = new TrisType("66", "+", "解锁"); ListTris.Add(tristype);
            tristype = new TrisType("69", "+", "其他修正"); ListTris.Add(tristype);
            tristype = new TrisType("80", "+", "发票数量"); ListTris.Add(tristype);
            tristype = new TrisType("88", "-", "价值更改-"); ListTris.Add(tristype);
            tristype = new TrisType("89", "+", "价值更改"); ListTris.Add(tristype);
            tristype = new TrisType("IN", "+", "系统自动调节"); ListTris.Add(tristype);

        }
        public DataSet GetRawData(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            DataTable tableLocal = null;
            string VKPAUFNUM;
            //string WBSRTDATE ;
            //WBSRTDATE = dwABPOS["WBSRTDATE"].ToString().Trim();
            //WBENTDATE = dwABPOS["WBENTDATE"].ToString().Trim();
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                #region
                
                or1.CommandText = "select * from purchase_request ";
                #endregion
                setLocal = Connect2Proxy(or1);
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "GAAUFYARN";
                    SetDefault(tableLocal);
                    byte[] b;
                    string value;
                    string sTemp = "";
                    string Column = "";
                    string ARTICLE = "", WBBDPART, FACTORY, WBEWBESCHICH,COLORDES;
                    int PCNUM;
                    double WBEWMTR;
                    List<DataRow> dltRows = new List<DataRow>();
                    ListYARNSummary = new List<YARNSummary>();
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        Column = "GSGARNBEZ";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "GSCBEZ";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "FACTORY";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        /*setLocal = GetCOLORDES(setLocal, dwTemp);
                        if (FilterData(dwTemp, ref dltRows))
                        {
                            FACTORY = dwTemp["FACTORY"].ToString().Trim();
                            ARTICLE = dwTemp["ARTICLE"].ToString().Trim();
                            WBEWBESCHICH = dwTemp["WBEWBESCHICH"].ToString().Trim();
                            COLORDES = dwTemp["COLORDES"].ToString().Trim(); 
                            WBBDPART = dwTemp["WBBDPART"].ToString().Trim();
                            PCNUM = System.Convert.ToInt16(dwTemp["PCNUM"].ToString());
                            WBEWMTR = System.Convert.ToDouble(dwTemp["WBEWMTR"].ToString());
                            var query = from WGSummary wgsum in ListWGSummary
                                        where
                                       wgsum.FACTORY == FACTORY
                                            &&wgsum.ARTICLE == ARTICLE
                                        && wgsum.WBBDPART == WBBDPART
                                        && wgsum.WBEWBESCHICH == WBEWBESCHICH
                                        select wgsum;
                            WGSummary wgSum = null;
                            foreach (WGSummary wgsum in query)
                            {
                                wgSum = wgsum;
                                wgSum.PCNUM += PCNUM;
                                wgsum.WBEWMTR += WBEWMTR;
                            }
                            if (wgSum == null)
                            {
                                wgSum = new WGSummary(FACTORY,ARTICLE,WBEWBESCHICH,COLORDES, WBBDPART, PCNUM, WBEWMTR);
                                ListWGSummary.Add(wgSum);
                            }
                        }*/
                    }
                    /*foreach (DataRow dwTemp in dltRows)
                    {
                        dwTemp.Delete();
                    }
                    setLocal.AcceptChanges();
                    DataRow dwWGFSUM = null;
                    tbWGSummary.Clear();
                    foreach (WGSummary wgsum in ListWGSummary)
                    {
                        dwWGFSUM = tbWGSummary.NewRow(); 
                        dwWGFSUM["FACTORY"] = wgsum.FACTORY;
                        dwWGFSUM["ARTICLE"] = wgsum.ARTICLE;
                        dwWGFSUM["WBEWBESCHICH"] = wgsum.WBEWBESCHICH;
                        dwWGFSUM["COLORDES"] = wgsum.COLORDES;
                        dwWGFSUM["WBBDPART"] = wgsum.WBBDPART;
                        dwWGFSUM["WBEWMTR"] = wgsum.WBEWMTR;
                        dwWGFSUM["PCNUM"] = wgsum.PCNUM;
                        tbWGSummary.Rows.Add(dwWGFSUM);
                    }
                    set4Return.Merge(tbWGSummary);
                    */
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }
        public DataSet GetREQWEI(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataSet setResult = new DataSet();
            DataSet setTemp = new DataSet();
            DataTable tableLocal = null;
            string VKPAUFNUM;
            //string WBSRTDATE ;
            //WBSRTDATE = dwABPOS["WBSRTDATE"].ToString().Trim();
            //WBENTDATE = dwABPOS["WBENTDATE"].ToString().Trim();
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();
                #region

                or1.CommandText = "select FABRIC,TYPE,GAART,GSGARNBEZ,GAFARBE,GSCBEZ,REQWEI "
                    + " from (SELECT FABRIC,TYPE,GAART,GSGARNBEZ,GAFARBE,GSCBEZ, "
                    + " SUM(DECODE(ABPNEUDAT,0,-REQWEI,REQWEI)) "
                    + " OVER(PARTITION BY GAART,GAFARBE ORDER BY ABPNEUDAT,TYPE) as wei,REQWEI "
                    + " FROM purchase_request) where wei>0 ";
                #endregion
                setLocal = Connect2Proxy(or1);
                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    SetDefault(tableLocal);
                    byte[] b;
                    string value;
                    string sTemp = "";
                    string Column = "";
                    string ARTICLE = "", TYPE, YARN, YARNDES,COLOR,COLORDES;
                    double REQUESTWEI,WEI;
                    List<DataRow> dltRows = new List<DataRow>();
                    ListYARNSummary = new List<YARNSummary>();
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        Column = "GSGARNBEZ";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        Column = "GSCBEZ";
                        #region
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp[Column].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp[Column] = value;
                        #endregion
                        if (true)
                        {
                            ARTICLE = dwTemp["FABRIC"].ToString().Trim();
                            TYPE = dwTemp["TYPE"].ToString().Trim();
                            YARN = dwTemp["GAART"].ToString().Trim();
                            YARNDES = dwTemp["GSGARNBEZ"].ToString().Trim();
                            COLOR = dwTemp["GAFARBE"].ToString().Trim();
                            COLORDES = dwTemp["GSCBEZ"].ToString().Trim();
                            REQUESTWEI = System.Convert.ToDouble(dwTemp["REQWEI"].ToString());                           
                            var query = from YARNSummary wgsum in ListYARNSummary
                                        where
                                           wgsum.ARTICLE == ARTICLE
                                        && wgsum.TYPE == TYPE
                                        && wgsum.YARN == YARN
                                        && wgsum.COLOR == COLOR
                                        select wgsum;
                            YARNSummary wgSum = null;
                            foreach (YARNSummary wgsum in query)
                            {
                                wgSum = wgsum;
                                wgSum.REQUESTWEI += REQUESTWEI;
                            }
                            if (wgSum == null)
                            {
                                wgSum = new YARNSummary(ARTICLE, TYPE, YARN, YARNDES, COLOR, COLORDES, REQUESTWEI);
                                ListYARNSummary.Add(wgSum);
                            }
                        }
                    }
                    foreach (DataRow dwTemp in dltRows)
                    {
                        dwTemp.Delete();
                    }
                    setLocal.AcceptChanges();
                    DataRow dwYARNREQSUM = null;
                    tbYARNSummary.Clear();
                    foreach (YARNSummary wgsum in ListYARNSummary)
                    {
                        dwYARNREQSUM = tbYARNSummary.NewRow();
                        dwYARNREQSUM["ARTICLE"] = wgsum.ARTICLE;
                        dwYARNREQSUM["TYPE"] = wgsum.TYPE;
                        dwYARNREQSUM["YARN"] = wgsum.YARN;
                        dwYARNREQSUM["YARNDES"] = wgsum.YARNDES;
                        dwYARNREQSUM["COLOR"] = wgsum.COLOR;
                        dwYARNREQSUM["COLORDES"] = wgsum.COLORDES;
                        dwYARNREQSUM["REQUESTWEI"] = wgsum.REQUESTWEI;
                        tbYARNSummary.Rows.Add(dwYARNREQSUM);
                    }
                    set4Return.Merge(tbYARNSummary);
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return set4Return;
        }
        /*public DataSet GetCOLORDES(DataSet dsTarget, DataRow dwABPOS)
        {
            string TableName = "COLORDES";
            try
            {
                orlReadCommand = OracleDB.CreateCommand();
                orlReadCommand.CommandText = "SELECT DISTINCT SUBSTR(SDDATEN,2,30) AS COLORDES FROM SDDAT "
                    + " WHERE SUBSTR(SDKEY,1,2)='00' AND trim(SUBSTR(SDKEY,3,10))=:ABPBESCHICH  AND SDSA = '9998' "
                     ;
                orlReadCommand.Parameters.Add("ABPBESCHICH", OracleType.NVarChar).Value = dwABPOS["WBEWBESCHICH"].ToString().Trim();
                dsResult = Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = TableName;

                    byte[] b;
                    string value;
                    string ColumnName = "";
                    foreach (DataRow dw in dsResult.Tables["COLORDES"].Rows)
                    {
                        ColumnName = "COLORDES";
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dw[ColumnName].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dw[ColumnName] = value;

                        dwABPOS["COLORDES"] = dw["COLORDES"];
                    }
                }
                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    if (dsTarget.Tables.Contains(TableName))
                    {
                        dsTarget.Tables[TableName].Clear();
                    }
                    dsTarget = ImportDataTable(dsTarget, dtTemp);
                }
                dsTarget.AcceptChanges();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dsTarget;
        }*/
        /*private bool FilterData(DataRow dwWBBEW, ref List<DataRow> dltRows)
        {
            try
            {
                string WBEWBS = dwWBBEW["WBEWBS"].ToString().Trim();
                string WBEWFC = dwWBBEW["WBEWFC"].ToString().Trim();

                if (!TrisInclude.Contains(WBEWBS + ";"))
                {
                    if (WBEWBS == "13"
                        && WBEWFC == "AV10")
                    {
                        return true;
                    }
                    if (WBEWBS == "32"
                        && WBEWFC == "VV06")
                    {
                        return true;
                    }
                    dltRows.Add(dwWBBEW);
                    return false;
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return true;
        }*/
        public DataSet BuildWBBST(DataRow dwUI)
        {
            string progress = "查询完成";
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            try
            {
                EventDispatcher.Instance.RaiseStatus("查询开始", StatusEventType.StartProgress);
                setResult = GetREQWEI(dwUI);
                setReturn.Merge(setResult);
                setResult = GetRawData(dwUI);
                setReturn.Merge(setResult);
                EventDispatcher.Instance.RaiseStatus("查询完成", StatusEventType.StopProgress);
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            return setReturn;
        }
    }
}